package com.ptt.sqm.activity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.ksoap2.serialization.PropertyInfo;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.DatePickerDialog.OnDateSetListener;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnHoverListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SlidingDrawer;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.LinearLayout.LayoutParams;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.ListView;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.ptt.sqm.component.PagingScrollView;
import com.ptt.sqm.component.ScrollViewListener;
import com.ptt.sqm.component.Search;
import com.ptt.sqm.component.SpinnerObject;
import com.ptt.sqm.model.AssessItem;
import com.ptt.sqm.model.AssessItemChoice5018;
import com.ptt.sqm.model.AssessItemList;
import com.ptt.sqm.model.AssessItemPoint;
import com.ptt.sqm.model.Login;
import com.ptt.sqm.model.MakeAssess;
import com.ptt.sqm.model.SearchAssess;
import com.ptt.sqm.model.SearchAssessCache;
import com.ptt.sqm.model.SearchAssessData;
import com.ptt.sqm.model.SearchAssessList;
import com.ptt.sqm.util.AppUtil;
import com.ptt.sqm.util.WidgetUtil;
import com.ptt.sqm.ws.AsyncResponse;
import com.ptt.sqm.ws.DefaultTask;
import com.ptt.sqmonmobile.R;

/**
 * A placeholder fragment containing a simple view.
 */
public class SearchAssessFragment extends DefaultFragment implements AsyncResponse {
	/**
	 * The fragment argument representing the section number for this fragment.
	 */
	private static final String ARG_SECTION_NUMBER = "section_number";

	// Create Webservice class object
	private DefaultTask mTask = null;

	private ArrayList<SearchAssessData> arrAssess;

	private SearchAssessList searchs;
	private SearchAssessCache searchCache;

	private int currentPage;
	private int maxPage;

	private Search search;

	private AssessItemList assessItemList;

	private static SearchAssessFragment fragment;

	private View mProgressView;

	private View mContentView;

	private View mProgressSearchView;

	private View mContentSearchView;

	/**
	 * Returns a new instance of this fragment for the given section number.
	 */
	public static SearchAssessFragment newInstance(int sectionNumber) {
		fragment = new SearchAssessFragment();
		Bundle args = new Bundle();
		args.putInt(ARG_SECTION_NUMBER, sectionNumber);
		fragment.setArguments(args);

		return fragment;
	}

	public SearchAssessFragment() {
	}

	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		// Loading...
		mProgressView = view.findViewById(R.id.progress);
		mContentView = view.findViewById(R.id.container);
		mProgressSearchView = view.findViewById(R.id.search_view_progress);
		mContentSearchView = view.findViewById(R.id.search_view);

		// No data
		TextView mNoData = (TextView) getView().findViewById(R.id.no_data);
		mNoData.setVisibility(TextView.INVISIBLE);
		final ViewGroup mHasData = (ViewGroup) getView().findViewById(R.id.has_data);
		mHasData.setVisibility(ViewGroup.GONE);

		// Search Sliding
		final SlidingDrawer slide = (SlidingDrawer) view.findViewById(R.id.search_slider);

		final ViewGroup grid = (ViewGroup) slide.findViewById(R.id.content);

		// Top-to-bottom
		grid.setRotation(180);
		grid.setVisibility(LinearLayout.VISIBLE);
		slide.setRotation(180);
		slide.open();
		if (slide.isOpened()) {
			// TODO Auto-generated method stub
			ImageView handle = (ImageView) slide.findViewById(R.id.handle);
			handle.setImageResource(R.drawable.show);

			WidgetUtil.setupSlideOpened(getActivity(), mNoData, slide);
		}

		slide.setOnDrawerCloseListener(new OnDrawerCloseListener() {

			@Override
			public void onDrawerClosed() {
				// TODO Auto-generated method stub
				grid.setVisibility(LinearLayout.INVISIBLE);
				ImageView handle = (ImageView) slide.findViewById(R.id.handle);
				handle.setImageResource(R.drawable.hide);

				WidgetUtil.setupSlideClosed(getActivity(), mHasData);
			}
		});

		slide.setOnDrawerOpenListener(new OnDrawerOpenListener() {

			@Override
			public void onDrawerOpened() {
				// TODO Auto-generated method stub
				ImageView handle = (ImageView) slide.findViewById(R.id.handle);
				handle.setImageResource(R.drawable.show);

				WidgetUtil.setupSlideOpened(getActivity(), mHasData, slide);

			}
		});

		// Search Input
		search = Search.getInstance(getActivity(), getView(), gson, com);
		search.showSearchAssess(getActivity(), getView());

		Button searchBtn = (Button) getView().findViewById(R.id.seach_btn);
		Button clearBtn = (Button) getView().findViewById(R.id.clear_btn);

		searchBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				boolean isChosen = false;
				try {
					Spinner yearSpin = (Spinner) getView().findViewById(R.id.year_assess);
					Spinner projSpin = (Spinner) getView().findViewById(R.id.project_assess);
					Spinner cycleSpin = (Spinner) getView().findViewById(R.id.cycle_assess);
					Spinner statusSpin = (Spinner) getView().findViewById(R.id.status_assess);
					Spinner divSpin = (Spinner) getView().findViewById(R.id.div_assess);
					Spinner deptSpin = (Spinner) getView().findViewById(R.id.dept_assess);
					Spinner zoneSpin = (Spinner) getView().findViewById(R.id.zone_assess);
					EditText startDate = (EditText) getView().findViewById(R.id.start_assess_date);
					EditText endDate = (EditText) getView().findViewById(R.id.end_assess_date);

					clearSearchAssessData();

					PropertyInfo yearProp = new PropertyInfo();
					PropertyInfo projProp = new PropertyInfo();
					PropertyInfo cycleProp = new PropertyInfo();
					PropertyInfo statusProp = new PropertyInfo();
					PropertyInfo divProp = new PropertyInfo();
					PropertyInfo deptProp = new PropertyInfo();
					PropertyInfo zoneProp = new PropertyInfo();
					PropertyInfo stationProp = new PropertyInfo();
					PropertyInfo startProp = new PropertyInfo();
					PropertyInfo endProp = new PropertyInfo();
					PropertyInfo pageSize = new PropertyInfo();
					PropertyInfo pageNumber = new PropertyInfo();

					String year = "", project = "", cycle = "", div = "", dept = "", zone = "", start = "", end = "", status = "";

					if (yearSpin.getSelectedItemPosition() != 0) {
						year = ((SpinnerObject) yearSpin.getSelectedItem()).getValue();
						isChosen = true;
					}
					if (projSpin.getSelectedItemPosition() != 0) {
						project = ((SpinnerObject) projSpin.getSelectedItem()).getValue();
						isChosen = true;
					}
					if (cycleSpin.getSelectedItemPosition() != 0) {
						cycle = ((SpinnerObject) cycleSpin.getSelectedItem()).getValue();
						isChosen = true;
					}
					if (statusSpin.getSelectedItemPosition() != 0) {
						status = (String) statusSpin.getSelectedItem();
						isChosen = true;
					}
					if (divSpin.getSelectedItemPosition() != 0) {
						div = ((SpinnerObject) divSpin.getSelectedItem()).getValue();
						isChosen = true;
					}
					if (deptSpin.getSelectedItemPosition() != 0) {
						dept = ((SpinnerObject) deptSpin.getSelectedItem()).getValue();
						isChosen = true;
					}
					if (zoneSpin.getSelectedItemPosition() != 0) {
						zone = ((SpinnerObject) zoneSpin.getSelectedItem()).getValue();
						isChosen = true;
					}
					if (startDate.getText().toString().length() != 0) {
						start = AppUtil.formatter_EN.format(AppUtil.formatter_EN_WS.parse(startDate.getText().toString()));
						isChosen = true;
					}
					if (endDate.getText().toString().length() != 0) {
						end = AppUtil.formatter_EN.format(AppUtil.formatter_EN_WS.parse(endDate.getText().toString()));
						isChosen = true;
					}
					if (search.shipToSelected != null && search.shipToSelected.length() != 0) {
						isChosen = true;
					}
					if (!isChosen) {
						Toast.makeText(getActivity(), getString(R.string.alert_at_least_one_choice), Toast.LENGTH_SHORT).show();
						return;
					}

					yearProp.setName("YearID");
					yearProp.setValue(year);
					yearProp.setType(String.class);

					projProp.setName("ProjectID");
					projProp.setValue(project);
					projProp.setType(String.class);

					cycleProp.setName("CycleID");
					cycleProp.setValue(cycle);
					cycleProp.setType(String.class);

					divProp.setName("div");
					divProp.setValue(div);
					divProp.setType(String.class);

					deptProp.setName("dept");
					deptProp.setValue(dept);
					deptProp.setType(String.class);

					zoneProp.setName("zone");
					zoneProp.setValue(zone);
					zoneProp.setType(String.class);

					stationProp.setName("Shipto");
					stationProp.setValue(search.shipToSelected);
					stationProp.setType(String.class);

					startProp.setName("AssessDateFrom");
					startProp.setValue(start);
					startProp.setType(String.class);

					endProp.setName("AssessDateTo");
					endProp.setValue(end);
					endProp.setType(String.class);

					statusProp.setName("AssessStatus");
					statusProp.setValue(status);
					statusProp.setType(String.class);

					pageSize.setName("Page_Size");
					pageSize.setValue(getString(R.string.page_size));
					pageSize.setType(Integer.class);

					pageNumber.setName("Page_Number");
					pageNumber.setValue("1");
					pageNumber.setType(Integer.class);

					searchCache.YearId = yearProp;
					searchCache.ProjectID = projProp;
					searchCache.CycleID = cycleProp;
					searchCache.div = divProp;
					searchCache.dept = deptProp;
					searchCache.zone = zoneProp;
					searchCache.Shipto = stationProp;
					searchCache.AssessDateFrom = startProp;
					searchCache.AssessDateTo = endProp;
					searchCache.AssessStatus = statusProp;
					searchCache.PageSize = pageSize.getValue().toString();
					searchCache.PageNumber = pageNumber.getValue().toString();

					slide.close();
					WidgetUtil.showProgress(mContentView, mProgressView, true);

					resetHeader();

					mTask = new DefaultTask(com, getString(R.string.method_searchassessdata));
					mTask.delegate = fragment;
					mTask.execute(yearProp, projProp, cycleProp, divProp, deptProp, zoneProp, stationProp, startProp, endProp, statusProp, pageSize, pageNumber);
				} catch (ParseException pe) {
					Log.i(getString(R.string.method_searchassessdata), "ParseException");
				}

			}
		});

		clearBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				clearSearchAssess();
			}
		});

		GridLayout slidingView = (GridLayout) getView().findViewById(R.id.gridLayout1);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.search_assess, container, false);
		return rootView;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		((MainActivity) activity).onSectionAttached(getArguments().getInt(ARG_SECTION_NUMBER));
	}

	private class adtSearchMultipleLine extends BaseAdapter {
		private class ListViewCustomItem {
			public ImageView status;
			public TextView data1;
			public TextView data2;
			public TextView data3;
			public TextView data4;
			public TextView data5;
			public TextView data6;
			public TextView data7;
			public TextView data8;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return arrAssess.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public void unregisterDataSetObserver(DataSetObserver observer) {
			if (observer != null) {
				super.unregisterDataSetObserver(observer);
			}
		}

		@Override
		public View getView(final int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub
			arg1 = LayoutInflater.from(SearchAssessFragment.this.getActivity()).inflate(R.layout.search_listitem, null);

			ListViewCustomItem lsvItem = new ListViewCustomItem();
			lsvItem.status = (ImageView) arg1.findViewById(R.id.status);
			lsvItem.data1 = (TextView) arg1.findViewById(R.id.data1);
			lsvItem.data2 = (TextView) arg1.findViewById(R.id.data2);
			lsvItem.data3 = (TextView) arg1.findViewById(R.id.data3);
			lsvItem.data4 = (TextView) arg1.findViewById(R.id.data4);
			lsvItem.data5 = (TextView) arg1.findViewById(R.id.data5);
			lsvItem.data6 = (TextView) arg1.findViewById(R.id.data6);
			lsvItem.data7 = (TextView) arg1.findViewById(R.id.data7);
			lsvItem.data8 = (TextView) arg1.findViewById(R.id.data8);

			if (arrAssess.get(arg0).getStatus() != null) {
				if (arrAssess.get(arg0).getStatus().equals(AppUtil.ASSESS_STATUS_DRAFT)) {
					lsvItem.status.setImageResource(R.drawable.status_assess_draft);
				} else if (arrAssess.get(arg0).getStatus().equals(AppUtil.ASSESS_STATUS_SUBMIT)) {
					lsvItem.status.setImageResource(R.drawable.status_assess_submit);
				} else if (arrAssess.get(arg0).getStatus().equals(AppUtil.ASSESS_STATUS_NONE)) {
					lsvItem.status.setImageResource(R.drawable.status_assess_none);
				}
			} else {
				lsvItem.status.setImageResource(R.drawable.status_assess_submit);
			}

			if (arrAssess.get(arg0).getData1() != null) {
				lsvItem.data1.setText(arrAssess.get(arg0).getData1());
			}

			if (arrAssess.get(arg0).getData2() != null) {
				lsvItem.data2.setText(arrAssess.get(arg0).getData2());
			}

			if (arrAssess.get(arg0).getData3() != null) {
				lsvItem.data3.setText(arrAssess.get(arg0).getData3());
			}

			if (arrAssess.get(arg0).getData4() != null) {
				lsvItem.data4.setText(arrAssess.get(arg0).getData4());
			}

			if (arrAssess.get(arg0).getData5() != null) {
				lsvItem.data5.setText(arrAssess.get(arg0).getData5());
			}

			if (arrAssess.get(arg0).getData6() != null) {
				lsvItem.data6.setText(arrAssess.get(arg0).getData6());
			}

			if (arrAssess.get(arg0).getData7() != null) {
				lsvItem.data7.setText(arrAssess.get(arg0).getData7());
			}

			if (arrAssess.get(arg0).getData8() != null) {
				lsvItem.data8.setText(arrAssess.get(arg0).getData8());
			}

			// Go to MakeAssess
			arg1.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View view) {
					if (WidgetUtil.isLocationServiceAvailable(getActivity())) {
						assessItemList = new AssessItemList();
						assessItemList.stationId = arrAssess.get(arg0).getStationId();
						assessItemList.stationName = arrAssess.get(arg0).getStationName();
						assessItemList.locationId = arrAssess.get(arg0).getLocationId();
						assessItemList.projectId = arrAssess.get(arg0).getProjectId();
						assessItemList.projectName = arrAssess.get(arg0).getProjectName();
						assessItemList.cycleAssessId = arrAssess.get(arg0).getCycleAssessId();
						assessItemList.cycle = arrAssess.get(arg0).getCycle();
						assessItemList.yearAssessId = arrAssess.get(arg0).getYearAssessId();
						assessItemList.assessDataId = arrAssess.get(arg0).getAssessDataId();

						getAssessItemService(arrAssess.get(arg0).getProjectId(), arrAssess.get(arg0).getCycleAssessId());
					}

				}
			});

			return arg1;
		}
	}

	@Override
	public void processFinish(String method, Object output) {
		// TODO Auto-generated method stub
		try {
			if (method.equals(getString(R.string.method_listyear)))
				search.getListYear(method, output);
			else if (method.equals(getString(R.string.method_listproject)))
				search.getListProject(method, output);
			else if (method.equals(getString(R.string.method_listcycle)))
				search.getListCycle(method, output);
			else if (method.equals(getString(R.string.method_listzone)))
				search.getListZone(method, output);
			else if (method.equals(getString(R.string.method_liststation)))
				search.getListStation(method, output);
			else if (method.equals(getString(R.string.method_searchassessdata)))
				getSearchAssessData(method, output);
			else if (method.equals(getString(R.string.method_listassessitem)))
				getListAssessItem(method, output);
		} catch (Exception e) {
		}
	}

	private void getListAssessItem(String method, Object output) {
		// TODO Auto-generated method stub
		try {
			String tempStationId = assessItemList.stationId;
			String tempStationName = assessItemList.stationName;
			String tempLocationId = assessItemList.locationId;
			String tempProjId = assessItemList.projectId;
			String tempProjName = assessItemList.projectName;
			String tempCycleId = assessItemList.cycleAssessId;
			String tempCycle = assessItemList.cycle;
			String tempYearId = assessItemList.yearAssessId;
			String tempAssessDataId = assessItemList.assessDataId;

			assessItemList = (AssessItemList) gson.fromJson((String) output, AssessItemList.class);

			assessItemList.stationId = tempStationId;
			assessItemList.stationName = tempStationName;
			assessItemList.locationId = tempLocationId;
			assessItemList.projectId = tempProjId;
			assessItemList.projectName = tempProjName;
			assessItemList.cycleAssessId = tempCycleId;
			assessItemList.cycle = tempCycle;
			assessItemList.yearAssessId = tempYearId;
			assessItemList.assessDataId = tempAssessDataId;

			FragmentManager fragmentManager = getFragmentManager();

			fragmentManager.beginTransaction().addToBackStack(AppUtil.STACK_SEARCH_ASSESS)
				.replace(R.id.container, MakeAssessFragment.newInstance(menuList.getPosFrm_AssessData(), assessItemList, false)).commit();

		} catch (Exception e) {
			Log.i(method, "Call WS Failed");
			WidgetUtil.showProgress(mContentView, mProgressView, false);
			Toast.makeText(getActivity(), getString(R.string.no_data), Toast.LENGTH_SHORT).show();
		}

	}

	private void getSearchAssessData(String method, Object output) {
		// TODO Auto-generated method stub
		WidgetUtil.showProgress(mContentView, mProgressView, false);
		try {
			searchs = (SearchAssessList) gson.fromJson((String) output, SearchAssessList.class);

			arrAssess = new ArrayList<SearchAssessData>();
			SearchAssessData assess;
			for (SearchAssess search : searchs.SearchAssess) {
				assess = new SearchAssessData();
				assess.setStatus(search.Assess_Status);
				assess.setData1(search.StationId + "  " + search.StationName);
				assess.setData2(search.Projectname);
				assess.setData3(search.Cycle);
				assess.setData4(search.UserName);
				assess.setData5(search.Assessdate);
				assess.setData6(search.UserTypeName);
				assess.setData7(search.TotalPoint + "  " + getString(R.string.point));
				assess.setData8(search.UserTypeName);
				assess.setProjectId(search.projectid);
				assess.setProjectName(search.Projectname);
				assess.setCycleAssessId(search.CycleAssessId);
				assess.setCycle(search.Cycle);
				assess.setStationId(search.StationId);
				assess.setStationName(search.StationName);
				assess.setYearAssessId(search.YearId);
				assess.setLocationId(search.LOCATION_ID);
				assess.setAssessDataId(search.AssessDataId != null ? search.AssessDataId : AppUtil.EMPTY);
				arrAssess.add(assess);
			}

			TableLayout listSearch = (TableLayout) getView().findViewById(R.id.search_table);
			listSearch.removeAllViews();
			// Inflate your row "template" and fill out the fields.
			for (final SearchAssessData search : arrAssess) {
				TableRow row = (TableRow) LayoutInflater.from(SearchAssessFragment.this.getActivity()).inflate(R.layout.search_listitem, null);
				ImageView status = (ImageView) row.findViewById(R.id.status);
				TextView data1 = (TextView) row.findViewById(R.id.data1);
				TextView data2 = (TextView) row.findViewById(R.id.data2);
				TextView data3 = (TextView) row.findViewById(R.id.data3);
				TextView data4 = (TextView) row.findViewById(R.id.data4);
				TextView data5 = (TextView) row.findViewById(R.id.data5);
				TextView data6 = (TextView) row.findViewById(R.id.data6);
				TextView data7 = (TextView) row.findViewById(R.id.data7);
				TextView data8 = (TextView) row.findViewById(R.id.data8);
				if (search.getStatus() != null) {
					if (search.getStatus().equals(AppUtil.ASSESS_STATUS_DRAFT)) {
						status.setImageResource(R.drawable.status_assess_draft);
					} else if (search.getStatus().equals(AppUtil.ASSESS_STATUS_SUBMIT)) {
						status.setImageResource(R.drawable.status_assess_submit);
					} else if (search.getStatus().equals(AppUtil.ASSESS_STATUS_NONE)) {
						status.setImageResource(R.drawable.status_assess_none);
					}
				} else {
					status.setImageResource(R.drawable.status_assess_submit);
				}

				if (search.getData1() != null) {
					data1.setText(search.getData1());
				}

				if (search.getData2() != null) {
					data2.setText(search.getData2());
				}

				if (search.getData3() != null) {
					data3.setText(search.getData3());
				}

				if (search.getData4() != null) {
					data4.setText(search.getData4());
				}

				if (search.getData5() != null) {
					data5.setText(search.getData5());
				}

				if (search.getData6() != null) {
					data6.setText(search.getData6());
				}

				if (search.getData7() != null) {
					data7.setText(search.getData7());
				}

				if (search.getData8() != null) {
					data8.setText(search.getData8());
				}

				// Go to MakeAssess
				row.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View view) {
						if (WidgetUtil.isLocationServiceAvailable(getActivity())) {
							assessItemList = new AssessItemList();
							assessItemList.stationId = search.getStationId();
							assessItemList.stationName = search.getStationName();
							assessItemList.locationId = search.getLocationId();
							assessItemList.projectId = search.getProjectId();
							assessItemList.projectName = search.getProjectName();
							assessItemList.cycleAssessId = search.getCycleAssessId();
							assessItemList.cycle = search.getCycle();
							assessItemList.yearAssessId = search.getYearAssessId();
							assessItemList.assessDataId = search.getAssessDataId();
							getAssessItemService(search.getProjectId(), search.getCycleAssessId());
						}

					}
				});

				listSearch.addView(row);
			}
			listSearch.requestLayout();
			// adtSearchMultipleLine adt = new adtSearchMultipleLine();
			// listSearch.setAdapter(adt);

			// calculate begin - end
			int endNo = Integer.parseInt(searchCache.PageNumber) * Integer.parseInt(searchCache.PageSize);
			int beginNo = (endNo - Integer.parseInt(searchCache.PageSize)) + 1;
			if (endNo >= searchs.SearchAssess.get(0).Row_Count)
				endNo = searchs.SearchAssess.get(0).Row_Count;

			// set total page
			TextView totalpage = (TextView) getView().findViewById(R.id.total_page);
			String totalpageStr = getString(R.string.total_page);
			totalpageStr = totalpageStr.replaceAll("@0", String.valueOf(beginNo));
			totalpageStr = totalpageStr.replaceAll("@1", String.valueOf(endNo));
			totalpageStr = totalpageStr.replaceAll("@2", String.valueOf(searchs.SearchAssess.get(0).Row_Count));
			totalpage.setText(totalpageStr);

			if (currentPage == 0) {
				double doubleCount = (double) searchs.SearchAssess.get(0).Row_Count / Integer.parseInt(searchCache.PageSize);
				final int pageCount = (int) Math.ceil(doubleCount);

				for (int i = 1; i <= pageCount; i++) {
					final int noOfPage;

					noOfPage = i;
					final LinearLayout mLinearScroll = (LinearLayout) getView().findViewById(R.id.paging);
					final TextView page = new TextView(getActivity());

					LayoutParams lp = new LinearLayout.LayoutParams(50, 50);

					page.setTextColor(Color.WHITE);
					page.setText(String.valueOf(i));
					page.setGravity(Gravity.CENTER);
					page.setBackground(getResources().getDrawable(R.drawable.page_indicator));
					mLinearScroll.addView(page, lp);
					mLinearScroll.setBackgroundColor(getResources().getColor(R.color.search_bar));

					if (noOfPage == 1)
						page.setSelected(true);

					page.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// Page Selected Hilight
							for (int index = 0; index < mLinearScroll.getChildCount(); index++) {
								mLinearScroll.getChildAt(index).setSelected(false);
							}
							v.setSelected(true);

							if (noOfPage != currentPage) {
								currentPage = noOfPage;
								getPaging(Integer.parseInt(getString(R.string.page_size)), noOfPage);
							}
						}

					});
				}
				// init paging
				final PagingScrollView mScroll = (PagingScrollView) getView().findViewById(R.id.paging_scroll);

				ViewTreeObserver vto = mScroll.getViewTreeObserver();
				vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

					@Override
					public void onGlobalLayout() {
						// TODO Auto-generated method stub
						maxPage = mScroll.getChildAt(0).getMeasuredWidth() - mScroll.getMeasuredWidth();
						mScroll.getViewTreeObserver().removeGlobalOnLayoutListener(this);
					}

				});

				mScroll.setScrollViewListener(new ScrollViewListener() {

					@Override
					public void onScrollChanged(PagingScrollView scrollView, int x, int y, int oldx, int oldy) {
						// TODO Auto-generated method stub
						if (maxPage > 0) {
							ImageView mPrevious = (ImageView) getView().findViewById(R.id.previous_page);
							ImageView mNext = (ImageView) getView().findViewById(R.id.next_page);
							int max = scrollView.getChildAt(0).getMeasuredWidth() - scrollView.getMeasuredWidth();
							if (x <= 0) {
								mPrevious.setVisibility(ImageView.INVISIBLE);
								mNext.setVisibility(ImageView.VISIBLE);
							} else if (x >= max) {
								mPrevious.setVisibility(ImageView.VISIBLE);
								mNext.setVisibility(ImageView.INVISIBLE);
							} else {
								mPrevious.setVisibility(ImageView.VISIBLE);
								mNext.setVisibility(ImageView.VISIBLE);
							}
						}
					}
				});

				ImageView mPrevious = (ImageView) getView().findViewById(R.id.previous_page);
				ImageView mNext = (ImageView) getView().findViewById(R.id.next_page);

				if (pageCount > Integer.parseInt(getString(R.string.paging_size))) {
					mPrevious.setVisibility(ImageView.INVISIBLE);
					mNext.setVisibility(ImageView.VISIBLE);
				} else {
					mPrevious.setVisibility(ImageView.INVISIBLE);
					mNext.setVisibility(ImageView.INVISIBLE);
				}

			}

			TextView mNoData = (TextView) getView().findViewById(R.id.no_data);
			mNoData.setVisibility(TextView.GONE);
			ViewGroup mHasData = (ViewGroup) getView().findViewById(R.id.has_data);
			mHasData.setVisibility(ViewGroup.VISIBLE);

			Log.i(method, "Call WS Success");

		} catch (Exception e) {
			Log.i(method, "Call WS Failed");

			Toast.makeText(getActivity(), getString(R.string.not_found), Toast.LENGTH_LONG).show();

			TextView mNoData = (TextView) getView().findViewById(R.id.no_data);
			mNoData.setVisibility(TextView.VISIBLE);
			ViewGroup mHasData = (ViewGroup) getView().findViewById(R.id.has_data);
			mHasData.setVisibility(ViewGroup.GONE);
		}
		WidgetUtil.showProgress(mContentSearchView, mProgressSearchView, false);

	}

	public void getPaging(int size, int number) {
		WidgetUtil.showProgress(mContentSearchView, mProgressSearchView, true);
		PropertyInfo pageSize = new PropertyInfo();
		PropertyInfo pageNumber = new PropertyInfo();

		pageSize.setName("Page_Size");
		pageSize.setValue(String.valueOf(size));
		pageSize.setType(Integer.class);

		pageNumber.setName("Page_Number");
		pageNumber.setValue(String.valueOf(number));
		pageNumber.setType(Integer.class);

		searchCache.PageSize = String.valueOf(size);
		searchCache.PageNumber = String.valueOf(number);

		mTask = new DefaultTask(com, getString(R.string.method_searchassessdata));
		mTask.delegate = fragment;
		mTask.execute(searchCache.YearId, searchCache.ProjectID, searchCache.CycleID, searchCache.div, searchCache.dept, searchCache.zone, searchCache.Shipto, searchCache.AssessDateFrom,
			searchCache.AssessDateTo, searchCache.AssessStatus, pageSize, pageNumber);

	}

	public void clearSearchAssess() {
		search.shipToSelected = "";

		Spinner removeSpin = (Spinner) getActivity().findViewById(R.id.year_assess);
		removeSpin.setSelection(0);

		removeSpin = (Spinner) getActivity().findViewById(R.id.div_assess);
		removeSpin.setSelection(0);

		removeSpin = (Spinner) getActivity().findViewById(R.id.status_assess);
		removeSpin.setSelection(0);

		List<String> removearr = new ArrayList<String>();
		removearr.add(getString(R.string.header_spinner));
		ArrayAdapter<String> removeAdt = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, removearr);

		removeSpin = (Spinner) getActivity().findViewById(R.id.project_assess);
		removeSpin.setAdapter(removeAdt);

		removeSpin = (Spinner) getActivity().findViewById(R.id.cycle_assess);
		removeSpin.setAdapter(removeAdt);

		removeSpin = (Spinner) getActivity().findViewById(R.id.dept_assess);
		removeSpin.setAdapter(removeAdt);

		removeSpin = (Spinner) getActivity().findViewById(R.id.zone_assess);
		removeSpin.setAdapter(removeAdt);

		EditText removeDate = (EditText) getView().findViewById(R.id.start_assess_date);
		removeDate.setText("");
		removeDate = (EditText) getView().findViewById(R.id.end_assess_date);
		removeDate.setText("");

		AutoCompleteTextView autoComplete = (AutoCompleteTextView) getView().findViewById(R.id.station);
		autoComplete.setText("");

		clearSearchAssessData();
	}

	public void clearSearchAssessData() {
		currentPage = 0;

		searchCache = new SearchAssessCache();
		searchs = new SearchAssessList();

		// Clear Search Data
		ListView listSearch = (ListView) getView().findViewById(R.id.search_listview);
		arrAssess = new ArrayList<SearchAssessData>();
		// adtSearchMultipleLine adt = new adtSearchMultipleLine();
		// listSearch.setAdapter(adt);
		// ((BaseAdapter) listSearch.getAdapter()).notifyDataSetChanged();

		// Clear Paging
		LinearLayout mLinearScroll = (LinearLayout) getView().findViewById(R.id.paging);
		mLinearScroll.removeAllViews();

		TextView totalpage = (TextView) getView().findViewById(R.id.total_page);
		totalpage.setText("");

		ImageView mPrevious = (ImageView) getView().findViewById(R.id.previous_page);
		ImageView mNext = (ImageView) getView().findViewById(R.id.next_page);

		mPrevious.setVisibility(ImageView.INVISIBLE);
		mNext.setVisibility(ImageView.INVISIBLE);

	}

	public void resetHeader() {
		PagingScrollView pageScroll = (PagingScrollView) getView().findViewById(R.id.paging_scroll);
		LinearLayout page = (LinearLayout) getView().findViewById(R.id.paging);

		pageScroll.scrollTo(0, 0);
		currentPage = 0;
		maxPage = 0;
		page.removeAllViews();
		page.requestLayout();
	}

	protected void getAssessItemService(String project, String cycle) {
		// TODO Auto-generated method stub

		WidgetUtil.showProgress(mContentView, mProgressView, true);
		PropertyInfo projProp = new PropertyInfo();
		projProp.setName(AssessItem.ProjectId);
		projProp.setValue(project);
		projProp.setType(String.class);

		PropertyInfo cycleProp = new PropertyInfo();
		cycleProp.setName(AssessItem.Cycle);
		cycleProp.setValue(cycle);
		cycleProp.setType(String.class);

		// Log.i("ProjectId", project);
		// Log.i("Cycle", cycle);

		// Toast.makeText(getActivity().getApplicationContext(), project +
		// AppUtil.SPACE + cycle, Toast.LENGTH_SHORT).show();
		mTask = new DefaultTask(com, getString(R.string.method_listassessitem));
		mTask.delegate = this;
		mTask.execute(projProp, cycleProp);

	}

	@Override
	public void processFinish(String method, Object output, Object... objects) {
		// TODO Auto-generated method stub

	}
}
